import { createRouter, createWebHashHistory } from 'vue-router';
import ManageLayout from '@/layout/LayoutView.vue';
import useLogin from '@/stores/login';
import NotFound from '@/views/error/404.vue';

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '/',
    component: ManageLayout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/index.vue'),
        meta: {
          title: '首页',
          icon: 'home.png'
        }
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('@/views/user/index.vue'),
        meta: { title: '用户管理', icon: 'user.png' }
      },
      {
        path: '/user/details/:uid',
        name: 'userDetails',
        component: () => import('@/views/user/details/index.vue'),
        meta: { title: '用户详情', hidden: true }
      },
      {
        path: '/blacklist',
        name: 'blacklist',
        component: () => import('@/views/blacklist/index.vue'),
        meta: { title: '黑名单', icon: 'blacklist.png' },
      },
      {
        path: '/taglibrary',
        name: 'taglibrary',
        meta: {
          title: '标签库',
          icon: 'label.png',
        },
        children: [
          {
            path: '/labellist',
            name: 'labellist',
            component: () => import('@/views/tag/list/index.vue'),
            meta: { title: '标签列表' },
          },
          {
            path: '/labelclassify',
            name: 'labelclassify',
            component: () => import('@/views/tag/classify/index.vue'),
            meta: { title: '标签分类' },
          },
        ],
      },
      {
        path: '/plat',
        name: 'plat',
        meta: {
          title: '平台审核',
          icon: 'plat.png',
        },
        children: [
          {
            path: '/activityreview',
            name: 'activityreview',
            component: () => import('@/views/audit/activityReview/index.vue'),
            meta: { title: '活动审核' },
          },
          {
            path: '/communityreview',
            name: 'communityreview',
            component: () => import('@/views/audit/community/index.vue'),
            meta: { title: '社群审核' },
          },
        ],
      },
      {
        path: '/activityments',
        name: 'activityments',
        meta: { title: '活动管理', icon: 'activityments.png' },
        children: [
          {
            path: '/activityment/:communityId?',
            name: 'activityment',
            component: () => import('@/views/activity/ment/index.vue'),
            meta: { title: '活动管理' },
            children: [
              {
                path: 'member/:id',
                name: 'activitymentMember',
                component: () => import('@/views/activity/ment/Member/index.vue'),
                meta: { title: '活动成员' },
              },
              {
                path: 'data/:id',
                name: 'activitymentData',
                component: () => import('@/views/activity/ment/data/index.vue'),
                meta: { title: '活动数据' },
              },
              {
                path: 'evaluate/:id',
                name: 'activitymentEvaluate',
                component: () => import('@/views/activity/ment/evaluate/index.vue'),
                meta: { title: '活动评价' },
              },
            ],
          },
          {
            path: '/activityclassification',
            name: 'activityclassification',
            component: () =>import('@/views/activity/category/index.vue'),
            meta: { title: '活动分类管理' },
          },
          {
            path: '/activeLibrary',
            name: 'activeLibrary',
            component: () => import('@/views/activity/library/index.vue'),
            meta: { title: '活动库管理' },
          },
          {
            path: '/venueLibrary',
            name: 'venueLibrary',
            component: () => import('@/views/activity/venue/index.vue'),
            meta: { title: '场地库管理' },
            children: [
              {
                path: '/venueType',
                name: 'venueType',
                component: () => import('@/views/activity/venue/type/index.vue'),
                meta: { title: '场地类型管理' },
              },
            ],
          },
          {
            path: '/expertDatabase',
            name: 'expertDatabase',
            component: () => import('@/views/activity/expert/index.vue'),
            meta: { title: '达人库管理' },
            children: [
              {
                path: '/expertType',
                name: 'expertType',
                component: () => import('@/views/activity/expert/type/index.vue'),
                meta: { title: '达人类型管理' },
              },
            ],
          },
          {
            path: '/supplyChain',
            name: 'supplyChain',
            component: () => import('@/views/activity/supply/index.vue'),
            meta: { title: '供应链管理' },
            children: [
              {
                path: '/supplyType',
                name: 'supplyType',
                component: () => import('@/views/activity/supply/type/index.vue'),
                meta: { title: '供应链类型管理' },
              },
            ],
          },
        ],
      },
      {
        path: '/site',
        name: 'site',
        meta: { title: '场地管理', icon: 'site.png' },
        children: [
          {
            path: '/spacelist',
            name: 'spacelist',
            component: () => import('@/views/venue/space/index.vue'),
            meta: { title: '空间列表' },
          },
          {
            path: '/facilitylist',
            name: 'facilitylist',
            component: () => import('@/views/venue/facility/index.vue'),
            meta: { title: '设施列表' },
          },
          {
            path: '/videolist',
            name: 'videolist',
            component: () => import('@/views/venue/video/index.vue'),
            meta: { title: '视频列表' },
          },
        ],
      },
      {
        path: '/association',
        name: 'association',
        meta: { title: '社群管理', icon: 'association.png' },
        children: [
          {
            path: '/communityment',
            name: 'communityment',
            component: () => import('@/views/association/ment/index.vue'),
            meta: { title: '社群管理' },
            children: [
              {
                path: 'member/:id',
                name: 'communitymentMember',
                component: () => import('@/views/association/ment/Member/index.vue'),
                meta: { title: '社群成员' },
              },
              {
                path: 'messagewall/:id',
                name: 'MessageWall',
                component: () => import('@/views/association/ment/MessageWall/index.vue'),
                meta: { title: '留言墙' },
              },
              {
                path: 'supergroup/:id',
                name: 'SuperGroup',
                component: () => import('@/views/association/ment/SuperGroup/index.vue'),
                meta: { title: '超级群' },
              },
              {
                path: 'resource/:id',
                name: 'resource',
                component: () => import('@/views/association/ment/Resource/index.vue'),
                meta: { title: '资源消息群' },
              }
            ],
          },
          {
            path: '/communityfl',
            name: 'communityfl',
            component: () => import('@/views/association/fl/index.vue'),
            meta: { title: '社群分类管理' },
          },
        ],
      },
      {
        path: '/order',
        name: 'order',
        component: () => import('@/views/order/index.vue'),
        meta: { title: '订单管理', icon: 'order.png' },
      },
      {
        path: '/setup',
        name: 'setup',
        meta: { title: '设置', icon: 'set.png' },
        children: [
          {
            path: '/privacypolicy',
            name: 'privacypolicy',
            component: () => import('@/views/setting/privacy/index.vue'),
            meta: { title: '隐私政策' },
          },
          {
            path: '/useagreement',
            name: 'useagreement',
            component: () => import('@/views/setting/agreement/index.vue'),
            meta: { title: '用户协议' },
          },
          {
            path: '/aboutus',
            name: 'aboutus',
            component: () => import('@/views/setting/about/index.vue'),
            meta: { title: '关于我们' },
          },
          {
            path: '/meetproblem',
            name: 'meetproblem',
            component: () => import('@/views/setting/faq/index.vue'),
            meta: { title: '遇到问题' },
          },
          {
            path: '/notificationmessage',
            name: 'notificationmessage',
            component: () => import('@/views/setting/notification/index.vue'),
            meta: { title: '通知消息模板' },
          },
        ],
      },
      {
        path: '/permission',
        name: 'permission',
        component: () => import('@/views/permission/index.vue'),
        meta: { title: '权限', icon: 'permission.png' },
        children: [
          {
            path: '/permissionUser',
            name: 'permissionUser',
            component: () => import('@/views/permission/user/index.vue'),
            meta: { title: '用户管理' },
          },
          {
            path: '/role',
            name: 'role',
            component: () => import('@/views/permission/role/index.vue'),
            meta: { title: '角色管理' },
            children: [
              {
                path: 'assign-users/:id',
                name: 'assignUsers',
                component: () => import('@/views/permission/role/assign-users/index.vue'),
                meta: { title: '分配用户' }
              }
            ]
          },
          {
            path: '/log',
            name: 'operationLog',
            component: () => import('@/views/permission/log/index.vue'),
            meta: { title: '操作日志' },
          },
        ],
      },
    ]
  },
  {
    path: '/404',
    name: 'NotFound',
    component: NotFound
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
];
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
});

// 路由前置守卫
router.beforeEach((to, from, next) => {
  const useStore = useLogin();
  const token = useStore.token;
  if (!token && to.path !== '/login') {
    next('/login');
  } else if (token && to.path === '/login') {
    next('/home');
  } else {
    next();
  }
});
export default router;
